<template>
  <div>
    <!-- 卡片区域 -->
    <el-card shadow="hover">
      <el-row :gutter="5">
        <!-- 会员查询 -->
        <el-col :span="6">
          <el-input
            placeholder="请输入分类名称"
            v-model="queryInfo.query"
            @clear="getCategoryList"
            clearable
          ></el-input>
        </el-col>
        <!-- 查询按钮 -->
        <el-col :span="4">
          <el-button type="primary" @click="getCategoryList">查询</el-button>
        </el-col>
      </el-row>
    </el-card>
    <!-- 表格区域 -->
    <el-table
      :data="res"
      border
      :row-class-name="tableRowClassName"
      size="mini"
      :header-cell-style="{background:'#ffd0bd',color:'#606266'}"
      v-loading="loading"
    >
      <el-table-column type="index" label="#" align="center"></el-table-column>
      <el-table-column prop="name" label="分类名称" align="center"></el-table-column>
      <el-table-column prop="updated_time" label="修改时间" align="center"></el-table-column>
      <el-table-column prop="created_time" label="创建时间" align="center"></el-table-column>
      <el-table-column label="操作" width="145%" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑|弹出表单 -->
    <el-dialog title="编辑内容" :visible.sync="editVisible">
      <el-form :model="editForm" >
        <el-form-item label="分类" :label-width="formLabelWidth">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">保 存</el-button>
      </div>
    </el-dialog>

    <!-- 页码 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[10, 20]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>

<script>
export default {
  data() {
    return {
      // 获取用户列表的参数对象
      queryInfo: {
        query: '',
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少数据
        pagesize: 10
      },
      editVisible: false,
      editForm: {},
      formLabelWidth: '80px',
      res: [],
      total: 0,
      loading: true
    }
  },
  methods: {
    tableRowClassName({ rowIndex }) {
      if (rowIndex === 1) {
        return 'warning-row'
      } else if (rowIndex === 3) {
        return 'success-row'
      }
      return ''
    },
    async getCategoryList() {
      this.loading = true
      const path = this.URL.Categories
      let temp = []
      const { data: result } = await this.$http
        .get(path, {
          params: this.queryInfo
        })
        .catch(() => {
          this.$message.error('NETWORK ERROR')
        })
      this.loading = false
      console.log(result)
      result.categories.forEach(element => {
        temp.push(JSON.parse(element))
      })
      this.res = temp
      this.total = result.total
      console.log(this.res)
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pagesize = newSize
      // 重新请求
      this.getCategoryList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.pagenum = newPage
      // 重新请求
      this.getCategoryList()
    },
    // 编辑操作
    handleEdit(row) {
      this.editVisible = true
      this.editForm = row
    },
    // 保存编辑
    async saveEdit() {
      this.editVisible = false
      const path = this.URL.Categories
      const { data: res } = await this.$http
        .patch(path, {
          data: {
              ...this.editForm
          }
        })
        .catch(() => {
          this.$message.error('NETWORK ERROR')
        })
      if (res.status !== 200) {
        this.$message.error('修改失败，请重试')
      }
      this.$message.success('保存成功')
    },
    // 删除
    async handleDelete(id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(() => {
        this.$message.error('NETWORK ERROR')
      })

      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除')
      }
      console.log(confirmResult, id)

      // delete请求
      const path = this.URL.Categories
      const { data: res } = await this.$http
        .delete(path, {
          params: { id: id }
        })
        .catch(() => {
          this.$message.error('NETWORK ERROR')
        })
      if (res.status !== 200) {
        this.$message.error('删除失败')
      }
      this.$message.success('删除成功')
      this.getCategoryList()
    },
    // 性别格式化
    formatSex(row) {
      return row.sex === 1 ? '男' : '女'
    }
  },
  created() {
    this.getCategoryList()
  }
}
</script>

<style lang="less" scoped>
.state {
  text-align: center;
}
</style>